package com.dmooo.yxmmerchant.view.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.dmooo.yxmmerchant.R;
import com.dmooo.yxmmerchant.adapter.AccountAdapter;
import com.dmooo.yxmmerchant.base.BaseActivity;
import com.dmooo.yxmmerchant.util.ListDataSave;
import com.dmooo.yxmmerchant.util.SlideRecyclerView;
import com.dmooo.yxmmerchant.view.model.AccPass;
import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import butterknife.BindView;

public class ChangeAccountActivity extends BaseActivity {
    @BindView(R.id.recy_focus)
    SlideRecyclerView recyFocus;
    @BindView(R.id.tv_left)
    TextView tvLeft;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.addAccount)
    Button addAccount;
    List<AccPass> accPassesList = new ArrayList<>();
    AccountAdapter adapter;
    private int page = 1;

    List<AccPass> classTests=new ArrayList<>();

    ListDataSave dataSave;
    private List<Object> listBean;


    @Override
    protected int getLayout() {
        return R.layout.activity_change_acc;

    }

    @Override
    protected void initEventAndData() {
        tvLeft.setVisibility(View.VISIBLE);
        tvTitle.setText("切换账号");
        listBean = new ArrayList<>();
        dataSave = new ListDataSave(ChangeAccountActivity.this, "baiyu");
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyFocus.setLayoutManager(linearLayoutManager);
        recyFocus.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        initAdapter();
        adapter = new AccountAdapter(ChangeAccountActivity.this,R.layout.item_account, classTests);
        recyFocus.setAdapter(adapter);
        recyFocus.closeMenu();
        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Intent intent=new Intent(ChangeAccountActivity.this,LoginActivity.class);
                Bundle bundle=new Bundle();
                bundle.putString("mername",classTests.get(position).getMername());
                bundle.putString("acount",classTests.get(position).getAccount());
                bundle.putString("password",classTests.get(position).getPassword());
                intent.putExtras(bundle);
                startActivity(intent);
                finish();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        tvLeft.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        addAccount.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Bundle bundle=new Bundle();
                Intent intent=new Intent(ChangeAccountActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });

        adapter.setOnDeleteClickListener(new AccountAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {
                cancelMallCollectRequest(position);
            }
        });
    }

    @Override
    protected void createPresenter() {

    }

    private void initAdapter(){
        classTests=new ArrayList<>();
        try{
            for(int i=0;i<dataSave.getDataList("javaBean").size();i++){
                classTests.add(new Gson().fromJson(dataSave.getDataList("javaBean").get(i).toString(), AccPass.class));
            }
            remove(classTests);
        }catch (Exception e){
            e.printStackTrace();
        }
//        classTests=new ArrayList<>();
//        if(dataSave.getDataList("javaBean").size()>0){
//            try{
//                for(int i=0;i<dataSave.getDataList("javaBean").size();i++){
//                    classTests.add(new Gson().fromJson(dataSave.getDataList("javaBean").get(i).toString(), AccPass.class));
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//            try{
//                for (int i = 0; i < classTests.size(); i++)  //外循环是循环的次数
//                {
//                    for (int j = classTests.size() - 1 ; j > i; j--)  //内循环是 外循环一次比较的次数
//                    {
//                        if (classTests.get(i).getMername().equals( classTests.get(j).getMername()))
//                        {
//                            classTests.remove(j);
//                        }
//                    }
//                }
//            }catch (Exception e){
//                e.printStackTrace();
//            }
//        }
    }

    //去Bean类重
    public void remove(List<AccPass> list){
        Log.e("before del depulicate",list.size()+"");
        Set<AccPass> set = new HashSet<>();
        List<AccPass> listaaa = new ArrayList<>();
        for (Iterator<AccPass> ite = list.iterator(); ite.hasNext();){
            AccPass aaa = ite.next();
            if (set.add(aaa)){
                listaaa.add(aaa);
            }
        }
        list.clear();
        list.addAll(listaaa);
    }

    private void cancelMallCollectRequest(int position){
        listBean=dataSave.getDataList("javaBean");
        if(listBean.size()<=1){
            Toast.makeText(this,"最少保留一个账号",Toast.LENGTH_LONG).show();
        }else{
            listBean.remove(position);
            dataSave.setDataList("javaBean",listBean);
            classTests.remove(position);
            adapter.notifyDataSetChanged();
        }
        recyFocus.closeMenu();
    }


}
